<?php
class Model_user extends CI_Model{
    function validate(){
        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));
        $q1 = $this->db->query("SELECT * FROM user WHERE username='$username' AND password='$password'");
        
        if($q1->num_rows == 1){
            $row = $q1->row();
            $creds = array(
                'type' => $row->level,
                'id' => $row->id_user,
                'nama' => $row->nama,
                'username' => $row->username
            );
            $this->session->set_userdata($creds);
        }else{
            $creds = null;
        }
        
        return $creds;
    }
    
    function auth() {
        if (isset($this->session->userdata['type'])) {
            $authenticated = 1;
        } else {
            $authenticated = 0;
        }
        
        return $authenticated;
    }
    
    function fetch_user($type){
        $q = $this->db->query("SELECT id_user, username, nama, level, aktif FROM user WHERE level = '$type' AND aktif = 'Y'");
        return $q->result();
    }
    
    function search($type,$q,$field){
        $q = $this->db->query("SELECT id_user, username, nama, level, aktif FROM user WHERE level = '$type' AND $field LIKE '%$q%'");
        return $q->result();
    }
    
    function fetch_single($id){
        $q = $this->db->query("SELECT id_user, username, nama, level, aktif FROM user WHERE id_user = $id AND aktif = 'Y'");
        return $q->row();
    }
    
    function cek_username($q){
        $q = $this->db->query("SELECT id_user FROM user WHERE username = '$q'");
        return $q->num_rows();
    }
    
    function cek_nama($q){
        $q = $this->db->query("SELECT id_user FROM user WHERE nama = '$q'");
        return $q->num_rows();
    }
    
    function insert_user(){
        $data = array(
            'id_user' => null,
            'username' => $this->input->post('username'),
            'nama' => $this->input->post('nama'),
            'password' => md5($this->input->post('password')),
            'level' => $this->input->post('level'),
            'aktif' => 'Y'
        );
        return $this->db->insert('user', $data);
    }
    
    function update_user(){
        if($this->input->post('passcek')){
            $data = array(
                'username' => $this->input->post('username'),
                'nama' => $this->input->post('nama'),
                'password' => md5($this->input->post('password')),
                'level' => $this->input->post('level')
            );
        }else{
            $data = array(
                'username' => $this->input->post('username'),
                'nama' => $this->input->post('nama'),
                'level' => $this->input->post('level')
            );
        }
        $this->db->where('id_user', $this->input->post('id_user'));
        return $this->db->update('user', $data); 
    }
    
    function update_set(){
        if($this->input->post('passcek')){
            $data = array(
                'nama' => $this->input->post('nama'),
                'password' => md5($this->input->post('password'))
            );
        }else{
            $data = array(
                'nama' => $this->input->post('nama')
            );
        }
        $this->db->where('id_user', $this->input->post('id_user'));
        return $this->db->update('user', $data); 
    }
    
    function delete_user(){
        $this->db->where('id_user', $this->input->post('id_user'));
        return $this->db->delete('user'); 
    }
}
?>
